package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.net.MalformedURLException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import net.ConexionAServer;

public class MainWindow extends JFrame{
	private ConexionAServer url;  //  @jve:decl-index=0:

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel panelPrincipal = null;
	private JMenuBar jJMenuBar = null;
	private JMenu jMenu1 = null;
	private JTabbedPane jTabbedPane = null;
	private JPanel panel_URL = null;
	private JPanel panelTipoTag = null;
	private JPanel panelRegExpFiltro = null;
	private JPanel panelEligeSalida = null;
	private JPanel panelProceso = null;
	private JCheckBox checkHttpsEnabled = null;
	private JTextField urlField = null;
	private JPanel urlPanel = null;
	private JLabel urlLabel = null;
	private JTextField puertoField = null;
	private JPanel urlMainPanel = null;
	private JLabel usernameLabel = null;
	private JLabel authLabel = null;
	private JLabel labelPuerto = null;
	private JTextField usuarioField = null;
	private JLabel labelPassw = null;
	private JPasswordField fieldPass = null;
	private JCheckBox checkEnableHttpAuth = null;
	private JButton urlBotonUsar = null;

	private JDialog badUrlDlg = null;  //  @jve:decl-index=0:visual-constraint="572,132"
	private JPanel jDlgContentPane = null;
	private JLabel badUrlLabel = null;
	private JButton badUrlDlgBtn = null;
	private JLabel proxyLabel = null;

	private JComboBox proxyTypecomboBox = null;

	private JTextField proxyField = null;

	private JCheckBox checkCacheEnable = null;

	private JPanel jPanel = null;  //  @jve:decl-index=0:visual-constraint="599,12"

	/**
	 * @throws HeadlessException
	 */
	public MainWindow() {
		super();
		this.initialize();
	}
	
	private void initialize(){
		this.setSize(new Dimension(358, 362));
		this.setJMenuBar(getJJMenuBar());
		this.setContentPane(getPanelPrincipal());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("Webpage parser");
		this.setLocationRelativeTo(null);

		getUrlMainPanel().setVisible(false);
		url = new ConexionAServer();
	}

	/**
	 * This method initializes panelPrincipal	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelPrincipal() {
		if (panelPrincipal == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.BOTH;
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.weighty = 1.0;
			gridBagConstraints4.gridx = 0;
			panelPrincipal = new JPanel();
			panelPrincipal.setLayout(new GridBagLayout());
			panelPrincipal.add(getJTabbedPane(), gridBagConstraints4);
		}
		return panelPrincipal;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenu1());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu1	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenu1() {
		if (jMenu1 == null) {
			jMenu1 = new JMenu("Archivo");
		}
		return jMenu1;
	}

	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.addTab("1-URL y conexion", null, getPanel_URL(), null);
			jTabbedPane.addTab("2-Tipo de elemento", null, getPanelTipoTag(), null);
			jTabbedPane.addTab("3-Filtrar por expresion", null, getPanelRegExpFiltro(), null);
			jTabbedPane.addTab("4-Salida de datos", null, getPanelEligeSalida(), null);
			jTabbedPane.addTab("5-Procesar", null, getPanelProceso(), null);
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes panel_URL	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanel_URL() {
		if (panel_URL == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.gridy = 0;
			panel_URL = new JPanel();
			panel_URL.setLayout(new BorderLayout());
			panel_URL.add(getUrlPanel(), BorderLayout.NORTH);
			panel_URL.add(getUrlMainPanel(), BorderLayout.CENTER);
		}
		return panel_URL;
	}

	/**
	 * This method initializes panelTipoTag	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelTipoTag() {
		if (panelTipoTag == null) {
			panelTipoTag = new JPanel();
			panelTipoTag.setLayout(new GridBagLayout());
		}
		return panelTipoTag;
	}

	/**
	 * This method initializes panelRegExpFiltro	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelRegExpFiltro() {
		if (panelRegExpFiltro == null) {
			panelRegExpFiltro = new JPanel();
			panelRegExpFiltro.setLayout(new GridBagLayout());
		}
		return panelRegExpFiltro;
	}

	/**
	 * This method initializes panelEligeSalida	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelEligeSalida() {
		if (panelEligeSalida == null) {
			panelEligeSalida = new JPanel();
			panelEligeSalida.setLayout(new GridBagLayout());
		}
		return panelEligeSalida;
	}

	/**
	 * This method initializes panelProceso	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelProceso() {
		if (panelProceso == null) {
			panelProceso = new JPanel();
			panelProceso.setLayout(new GridBagLayout());
		}
		return panelProceso;
	}

	
	public boolean getUsaHttps(){
		return getCheckHttpsEnabled().isSelected();
	}


	/**
	 * This method initializes checkHttpsEnabled	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getCheckHttpsEnabled() {
		if (checkHttpsEnabled == null) {
			checkHttpsEnabled = new JCheckBox("Usar https");
			checkHttpsEnabled.setName("checkHttpsEnabled");
			checkHttpsEnabled.setEnabled(false);
			checkHttpsEnabled.setText("https");
			checkHttpsEnabled.setBounds(new Rectangle(12, 11, 84, 24));
		}
		return checkHttpsEnabled;
	}

	/**
	 * This method initializes urlField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getUrlField() {
		if (urlField == null) {
			urlField = new JTextField();
			urlField.setMinimumSize(new Dimension(40, 20));
			urlField.setText("http://");
		}
		return urlField;
	}

	/**
	 * This method initializes urlPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getUrlPanel() {
		if (urlPanel == null) {
			urlLabel = new JLabel();
			urlLabel.setText("  URL:  ");
			urlPanel = new JPanel();
			urlPanel.setLayout(new BorderLayout());
			urlPanel.add(getUrlField(), BorderLayout.CENTER);
			urlPanel.add(urlLabel, BorderLayout.WEST);
			urlPanel.add(getUrlBotonUsar(), BorderLayout.EAST);
		}
		return urlPanel;
	}

	/**
	 * This method initializes puertoField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPuertoField() {
		if (puertoField == null) {
			puertoField = new JTextField();
			puertoField.setEditable(false);
			puertoField.setName("puertoField");
			puertoField.setBounds(new Rectangle(13, 56, 79, 20));
			puertoField.setMinimumSize(new Dimension(30, 20));
		}
		return puertoField;
	}

	/**
	 * This method initializes authPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getUrlMainPanel() {
		if (urlMainPanel == null) {
			proxyLabel = new JLabel();
			proxyLabel.setText("Proxy:");
			labelPassw = new JLabel();
			labelPassw.setBounds(new Rectangle(165, 49, 50, 15));
			labelPassw.setEnabled(false);
			labelPassw.setText("Pass: ");
			labelPuerto = new JLabel();
			labelPuerto.setBounds(new Rectangle(14, 37, 78, 18));
			labelPuerto.setText("Puerto: ");
			authLabel = new JLabel();
			authLabel.setText("Credenciales http:");
			authLabel.setBounds(new Rectangle(163, 15, 103, 16));
			usernameLabel = new JLabel();
			usernameLabel.setText("Usuario: ");
			usernameLabel.setEnabled(false);
			usernameLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
			usernameLabel.setBounds(new Rectangle(165, 34, 50, 16));
			urlMainPanel = new JPanel();
			urlMainPanel.setLayout(null);
			urlMainPanel.setEnabled(true);
			urlMainPanel.setVisible(true);
			urlMainPanel.add(authLabel, null);
			urlMainPanel.add(usernameLabel, null);
			urlMainPanel.add(getCheckHttpsEnabled(), null);
			urlMainPanel.add(getPuertoField(), null);
			urlMainPanel.add(labelPuerto, null);
			urlMainPanel.add(getUsuarioField(), null);
			urlMainPanel.add(labelPassw, null);
			urlMainPanel.add(getFieldPass(), null);
			urlMainPanel.add(getCheckEnableHttpAuth(), null);
			urlMainPanel.add(getCheckCacheEnable(), null);
		}
		return urlMainPanel;
	}

	

	/**
	 * This method initializes usuarioField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getUsuarioField() {
		if (usuarioField == null) {
			usuarioField = new JTextField();
			usuarioField.setBounds(new Rectangle(213, 34, 96, 17));
			usuarioField.setEnabled(false);
		}
		return usuarioField;
	}

	/**
	 * This method initializes fieldPass	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getFieldPass() {
		if (fieldPass == null) {
			fieldPass = new JPasswordField();
			fieldPass.setBounds(new Rectangle(213, 50, 97, 16));
			fieldPass.setEnabled(false);
		}
		return fieldPass;
	}

	/**
	 * This method initializes checkEnableHttpAuth	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getCheckEnableHttpAuth() {
		if (checkEnableHttpAuth == null) {
			checkEnableHttpAuth = new JCheckBox();
			checkEnableHttpAuth.setBounds(new Rectangle(269, 15, 20, 16));
			checkEnableHttpAuth.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					url.usarAuth(getCheckEnableHttpAuth().isSelected());
					getUsuarioField().setEnabled(getCheckEnableHttpAuth().isSelected());
					getFieldPass().setEnabled(getCheckEnableHttpAuth().isSelected());
					usernameLabel.setEnabled(getCheckEnableHttpAuth().isSelected());
					labelPassw.setEnabled(getCheckEnableHttpAuth().isSelected());
				}
			});
		}
		return checkEnableHttpAuth;
	}

	/**
	 * This method initializes urlBotonUsar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getUrlBotonUsar() {
		if (urlBotonUsar == null) {
			urlBotonUsar = new JButton();
			urlBotonUsar.setText("Usar");
			urlBotonUsar.addActionListener(new java.awt.event.ActionListener() {   
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					try {
						url.inicializarConexion(getUrlField().getText());
						getUrlMainPanel().setVisible(true);
					} catch (MalformedURLException e1) {
						getBadUrlDlg().setVisible(true);
						getUrlMainPanel().setVisible(false);
					}
					getPuertoField().setText("" + url.getPuerto());
				}
			
			});
		}
		return urlBotonUsar;
	}

	/**
	 * This method initializes badUrlDlg	
	 * 	
	 * @return javax.swing.JDialog	
	 */
	private JDialog getBadUrlDlg() {
		if (badUrlDlg == null) {
			badUrlDlg = new JDialog(this);
			badUrlDlg.setSize(new Dimension(243, 180));
			badUrlDlg.setTitle("URL ERRONEA");
			badUrlDlg.setResizable(false);
			badUrlDlg.setVisible(false);
			badUrlDlg.setContentPane(getJDlgContentPane());
		}
		return badUrlDlg;
	}

	/**
	 * This method initializes jDlgContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJDlgContentPane() {
		if (jDlgContentPane == null) {
			badUrlLabel = new JLabel();
			badUrlLabel.setText("Ha introducido una URL invalida");
			badUrlLabel.setBounds(new Rectangle(22, 35, 184, 16));
			jDlgContentPane = new JPanel();
			jDlgContentPane.setLayout(null);
			jDlgContentPane.add(badUrlLabel, null);
			jDlgContentPane.add(getBadUrlDlgBtn(), null);
		}
		return jDlgContentPane;
	}

	/**
	 * This method initializes badUrlDlgBtn	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBadUrlDlgBtn() {
		if (badUrlDlgBtn == null) {
			badUrlDlgBtn = new JButton();
			badUrlDlgBtn.setBounds(new Rectangle(58, 84, 120, 43));
			badUrlDlgBtn.setText("OK");
			badUrlDlgBtn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getBadUrlDlg().setVisible(false);
				}
			});
		}
		return badUrlDlgBtn;
	}

	/**
	 * This method initializes proxyTypecomboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getProxyTypecomboBox() {
		if (proxyTypecomboBox == null) {
			proxyTypecomboBox = new JComboBox();
			proxyTypecomboBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					//setear proxy
					if (proxyTypecomboBox.getSelectedItem().toString().equals("Ninguno")){
						getProxyField().setEnabled(false);
						url.usarProxy(false);
					} else { //con proxy
						getProxyField().setEnabled(true);
						if (proxyTypecomboBox.getSelectedItem().toString().equals("Socks")){
							
						} else if (proxyTypecomboBox.getSelectedItem().toString().equals("Http")){
							
						}
					}
						
				}
			});
			proxyTypecomboBox.addItem("Ninguno");
			proxyTypecomboBox.addItem("Socks");
			proxyTypecomboBox.addItem("Http");
		}
		return proxyTypecomboBox;
	}

	/**
	 * This method initializes proxyField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getProxyField() {
		if (proxyField == null) {
			proxyField = new JTextField();
			proxyField.setEnabled(false);
			proxyField.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyTyped(java.awt.event.KeyEvent e) {
					
				}
			});
		}
		return proxyField;
	}

	/**
	 * This method initializes checkCacheEnable	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getCheckCacheEnable() {
		if (checkCacheEnable == null) {
			checkCacheEnable = new JCheckBox();
			checkCacheEnable.setBounds(new Rectangle(14, 90, 111, 18));
			checkCacheEnable.setText("Habilitar caché");
			checkCacheEnable.addChangeListener(new javax.swing.event.ChangeListener() {
				public void stateChanged(javax.swing.event.ChangeEvent e) {
					url.setCache(getCheckCacheEnable().isSelected());
				}
			});
		}
		return checkCacheEnable;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints5.gridy = 3;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints3.gridy = 2;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 1;
			gridBagConstraints2.gridy = 0;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.setSize(new Dimension(224, 98));
			jPanel.add(proxyLabel, gridBagConstraints2);
			jPanel.add(getProxyTypecomboBox(), gridBagConstraints3);
			jPanel.add(getProxyField(), gridBagConstraints5);
		}
		return jPanel;
	}

}  //  @jve:decl-index=0:visual-constraint="128,16"
